package com.dc.seckill.redis;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @模块 redis缓存模块
 * @作者 djs
 * @时间 2018/3/14 15:59
 * @版本 V1.0
 * @描述 缓存业务类
 */
@Service
public class RedisService {

    public Logger log = LoggerFactory.getLogger(RedisService.class);

    @Autowired
    JedisPool jedisPool;

    /**
     * 根据key将缓存从redis取出
     * @params [prefix, key, clazz]
     * @returns T
     */
    public <T> T get(KeyPrefix prefix, String key,Class<T> clazz) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();

            String realkey = prefix.getPrefix()+key;
            String str = jedis.get(realkey);
            T t = stringToBean(str, clazz);
            return t;
        } finally {
            returnPool(jedis);
        }
    }

    /**
     * 根据key将缓存存入redis
     * @params [prefix, key, value]
     * @returns boolean
     */
    public <T> boolean set(KeyPrefix prefix, String key,T value){

        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            String str = beanToString(value);
            if(str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey = prefix.getPrefix()+key;
            int seconds = prefix.expireSeconds();
            if(seconds <= 0) {
                jedis.set(realKey, str);
            }else {
                jedis.setex(realKey, seconds, str);
            }
            return true;
        }catch (Exception e){
            log.error(e.toString()+e);
        }
        finally {
            returnPool(jedis);
        }
        return false;
    }

    /**
     * 增加值
     * @params [prefix, key]
     * @returns java.lang.Long
     */
    public <T> Long incr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            return  jedis.incr(realKey);
        }finally {
            returnPool(jedis);
        }
    }

    /**
     * 减少值
     * @params [prefix, key]
     * @returns java.lang.Long
     */
    public <T> Long decr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            return  jedis.decr(realKey);
        }finally {
            returnPool(jedis);
        }
    }

    /**
     * 判断该缓存是否存在
     * @params [prefix, key]
     * @returns boolean
     */
    public boolean exists(KeyPrefix prefix, String key){
        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            //生成真正的key
            String realkey = prefix.getPrefix()+key;
            return jedis.exists(realkey);
        }finally {
            returnPool(jedis);
        }
    }

    /**
     * 将要存储的对象转换为String类型的值
     * @params [value]
     * @returns void
     */
    private <T> String beanToString(T value) {
        if(value == null){
            return null;
        }
        Class clazz = value.getClass();
        if(Integer.class == clazz || int.class == clazz){
            return "" + value;
        }else if(String.class == clazz){
            return (String) value;
        }else if(Long.class == clazz || long.class == clazz ){
            return "" + value;
        }else{
            return JSON.toJSONString(value);
        }
    }

    /**
     * 将String类型的值转换为需要获取的对象
     * @params [str]
     * @returns T
     */
    private <T> T stringToBean(String str,Class clazz){
        if(str == null || str.length() <= 0 ||clazz == null){
            return null;
        }
        if(Integer.class == clazz || int.class == clazz){
            return (T) Integer.valueOf(str);
        }else if(String.class == clazz){
            return (T) str;
        }else if(Long.class == clazz || long.class == clazz ){
            return (T) Long.valueOf(str);
        }else{
            return (T) JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }

    /**
     * 将连接释放
     * @params [Jedis]
     * @returns void
     */
    private void returnPool(Jedis jedis) {
        if(jedis != null){
            jedis.close();
        }
    }

}
